package com.briup.server.security;

import com.briup.server.domain.vo.UserDetailVO;

import java.util.List;

/**
 * 认证信息应用上下文
 */
public class SecurityContext {
    private static final ThreadLocal<UserDetailVO> THREAD_LOCAL
            = new ThreadLocal<>();

    public static UserDetailVO currentUser() {
        return THREAD_LOCAL.get();
    }

    public static Long userId() {
        return currentUser().getId();
    }

    public static void setCurrentUser(UserDetailVO user) {
        THREAD_LOCAL.set(user);
    }

    /**
     * 获取当前用户的角色名称列表
     *
     * @return 角色名称列表
     */
    public static List<String> roleNames() {
        return currentUser().getRoles().stream()
                // 映射为角色名称并转换为小写
                .map(roleVO -> roleVO.getName().toLowerCase())
                .toList();
    }

    public static void clear() {
        THREAD_LOCAL.remove();
    }
}
